<template>
  <a-card title="数据模型">
    <div style="background: #fff; height: 100%; overflow: auto">
      <a-tree
        v-if="departTree.length > 0"
        @select="onSelect"
        :selectedKeys="selectedKeys"
        :treeData="departTree"
        :expandedKeys="iExpandedKeys"
        :checkStrictly="checkStrictly"
        @expand="onExpand"
      >
        <a-icon slot="switcherIcon" type="down" />
      </a-tree>
    </div>
  </a-card>
</template>

<script>
import { getAction, deleteAction } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'

export default {
  name: 'LeftTree',
  props: ['templateId', 'model'],
  mixins: [JeecgListMixin],
  data() {
    return {
      loading: false,
      autoExpandParent: true,
      departTree: [],
      selectedKeys: [],
      checkStrictly: true,
      url: '/dataModel/queryDataModelTemplate',
      modelType: '5',
      disableMixinCreated: true,
      iExpandedKeys: [],
    }
  },
  watch: {
    $route: {
      immediate: true,
      handler(to) {
        if (Object.keys(to.query).length) {
          this.selectedKeys = [to.query.id]
          this.iExpandedKeys = [to.query.pid]
          this.$emit('selected', to.query.id, to.query.record)
        } else if (Object.keys(to.params).length && to.params.from == 'sjxmss') {
          let { EngineeringInformation } = to.params
          this.selectedKeys = [EngineeringInformation.id]
          this.iExpandedKeys = [EngineeringInformation.pid]
          this.$emit('selected', EngineeringInformation.id, to.params.returnRecord)
        }
      },
    },
  },
  created() {
    this.loadTree()
  },
  methods: {
    loadTree() {
      var that = this
      that.departTree = []
      getAction(this.url, { funcType: this.modelType }).then((res) => {
        if (res.success) {
          //部门全选后，再添加部门，选中数量增多
          this.allTreeKeys = []
          this.departTree = this.getTree(res.result, '0')
          for (let i = 0; i < res.result.length; i++) {
            let temp = res.result[i]
            that.getAllKeys(temp)
          }
          this.loading = false
        }
      })
    },
    onExpand(expandedKeys) {
      console.log('onExpand', expandedKeys)
      this.iExpandedKeys = expandedKeys
    },
    getAllKeys(node) {
      // console.log('node',node);
      this.allTreeKeys.push(node.id)
      if (node.children && node.children.length > 0) {
        for (let a = 0; a < node.children.length; a++) {
          this.getAllKeys(node.children[a])
        }
      }
    },
    getTree(treeData, pid) {
      let treeArr = []
      for (let i = 0; i < treeData.length; i++) {
        let node = treeData[i]
        if (node.pid == pid) {
          let newNode = {
            title: node.modelName,
            key: node.id,
            pid: node.pid,
            children: this.getTree(treeData, node.id),
          }
          treeArr.push(newNode)
        }
      }
      return treeArr
    },
    onSelect(selectedKeys, e) {
      this.hiding = false
      let record = e.node.dataRef
      this.currSelected = Object.assign({}, record)
      this.selectedKeys = [record.key]
      if ((record.key == '817001' && record.title == '审计资产阅帐') || record.pid == '817001')
        this.$emit('selected', this.selectedKeys[0], '', false)
      else this.$emit('selected', this.selectedKeys[0], '', true)
    },
  },
}
</script>

<style lang="less" scoped>
.ant-card {
  margin: 0px 0px !important;

  /deep/ .ant-card-head {
    padding: 0 15px;

    /deep/ .ant-card-head-title,
    /deep/ .ant-card-extra {
      padding: 10px 0;
    }
  }

  /deep/ .ant-card-body {
    padding: 10px;
    height: calc(100vh - 276px);
  }
}
</style>